package com.qf.leadnewsbehavior.controller;

import com.qf.leadnewsbehavior.model.dtos.BehaviorDto;
import com.qf.leadnewsbehavior.service.RedisService;
import com.qf.leadnewsmodel.consts.BehaviorConst;
import com.qf.leadnewsmodel.dtos.ResponseResult;
import com.qf.leadnewsmodel.enums.AppHttpCodeEnum;
import com.qf.leadnewsutils.commons.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class BehaviorController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/read_behavior")
    public ResponseResult read(@RequestBody BehaviorDto behaviorDto){

        String key = BehaviorConst.ARTICLE_READ_PREFIX + behaviorDto.getArticleId();
        redisService.incr(key);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @RequestMapping("/likes_behavior")
    public ResponseResult likes(@RequestBody BehaviorDto behaviorDto){
        String key = BehaviorConst.ARTICLE_LIKES_PREFIX + behaviorDto.getArticleId();
        //bitmap的偏移量
        Long offset = ThreadLocalUtil.getUid();
        //bitmap对应的值 0 -- true（点赞）  1 -- false(取消点赞)
        Integer ops = behaviorDto.getOperation();
        redisService.bitmap(key,offset,ops);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @RequestMapping("/un_likes_behavior")
    public ResponseResult un_likes(@RequestBody BehaviorDto behaviorDto){
        String key = BehaviorConst.ARTICLE_UN_LIKES_PREFIX + behaviorDto.getArticleId();
        Long offset = ThreadLocalUtil.getUid();
        //bitmap对应的值 0 -- true（不喜欢）  1 -- false(取消不喜欢)
        Integer ops = behaviorDto.getType();
        redisService.bitmap(key,offset,ops);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
